function $(id) {
      return document.getElementById(id)
}

window.onload = function(){
    findByItem(1);
}

function findByItem(pageNo) {
    axios.get("/room/findByItem",{
        params:{
            pageNo:pageNo,
            site:$("address").value,
            state:$("state").value,
            canLive:$("canLive").value,
            type:$("type").value
        }
    }).then(resp=>{
        var str = "";
        var list = resp.data.list;
        list.forEach(e=>{
            str += `<tr><td>${e.site}</td><td>${e.maxNum}</td><td>${e.num}</td><td>${e.state}</td>
        <td>${e.type}</td><td>
        <input value="删除" type="button" onclick="delRoom(${e.id})">
        <input value="查看房间信息" type="button" onclick="findById(${e.id})">
        </td></tr>`
            $("roomData").innerHTML=str;
        })
        var pageObj = "";
        for (var i=1;i<=resp.data.pages;i++){
            pageObj += `<a style="margin-left: 10px" href="javascript:findByItem(${i})">${i}</a>`
            $("pageDiv").innerHTML=pageObj;
        }
    })
}

function fileChange(){
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL(document.getElementById("myFile").files[0]);
    reader.onload = function (ev) {
        document.getElementById("roomImg").src = ev.target.result;
    }
}
function showAddRoom() {
    $("addDiv").style.visibility="visible";
    $("addDiv").style.width = document.documentElement.clientWidth+"px";
    $("addDiv").style.height = document.documentElement.clientHeight+"px";
}

function addRoom() {
    var params = new FormData();
    params.append("site",$("addressInfo").value);
    params.append("maxNum",$("maxNumInfo").value);
    params.append("type",$("addType").value);
    params.append("roomImg",$("myFile").files[0]);
    let config = {
        headers: {'Content-Type': 'multipart/form-data' }
    }
    axios.post("/room/add",params,config).then(resp=>{
        if (resp.data=="ok"){
            findByItem(1);
            $("addressInfo").value='';
            $("maxNumInfo").value='';
            $("maxNumInfo").value='';
            $("myFile").value='';
            $("addDiv").style.visibility="hidden";
        }
    })
}

function delRoom(id) {
    axios.get("/room/del",{
        params:{
            id:id
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findByItem(1);
        }
    })
}
function findById(id) {
    $("showDiv").style.visibility="visible";
    $("showDiv").style.width = document.documentElement.clientWidth+"px";
    $("showDiv").style.height = document.documentElement.clientHeight+"px";
    axios.get("/room/findById",{
        params:{
            id:id
        }
    }).then(resp=>{
        var obj = resp.data;
        $("showSite").innerHTML = obj.site;
        $("showMaxNum").innerHTML = obj.maxNum;
        $("showState").innerHTML = obj.state;
        $("showNum").innerHTML = obj.num;
        $("showType").innerHTML = obj.type;
        $("showImage").src = "/html/img/"+obj.image;
    })
}
function hiddenDiv() {
    $("showSite").innerHTML = '';
    $("showMaxNum").innerHTML = '';
    $("showState").innerHTML = '';
    $("showNum").innerHTML ='' ;
    $("showType").innerHTML = '';
    $("showImage").src = '';
    $("showDiv").style.visibility="hidden";
}